package com.jerome.fs.server;

import com.jerome.fs.constants.AppConstant;
import com.jerome.fs.http.HttpMethodEnum;
import com.jerome.fs.http.Response;
import com.jerome.fs.http.client.HttpClient;
import com.jerome.fs.server.aggregation.ContextUtil;
import com.jerome.fs.server.aggregation.handler.SlaveSyncClientHandler;
import com.jerome.fs.sync.SyncClient;
import com.jerome.fs.storage.ExpiringMap;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * GET Client
 * @author Jerome
 * @date 2023/7/31
 */
public class GETClient {

    public static void main(String[] args) {
        if (args.length ==0) {
            System.out.println("Please provide some command-line arguments.\ntips:java -cp work2.jar com.jerome.fs.server.GETClient 127.0.0.1:4567");
        } else {
            String url =  args[0];
            if(!url.startsWith(AppConstant.HTTP_PREFIX)){
                url = AppConstant.HTTP_PREFIX+url;
            }
            //Register heartbeat detection
            try {
                new SyncClient(url,new SlaveSyncClientHandler(), HttpMethodEnum.PINGPONG);
            } catch (MalformedURLException e) {
                System.out.println("Register heartbeat, MalformedURLException:"+e.getMessage());
            } catch (IOException e) {
                System.out.println("Register heartbeat, IOException:"+e.getMessage());
            }
            System.out.println("Enter 'q' to exit");
            System.out.println("Please enter the weather data ID you want to query, separated by multiple available spaces.");
            Scanner scan = new Scanner(System.in);
            AtomicInteger no = new AtomicInteger(0);
            while (true){
                try {
                    String in = scan.nextLine();
                    if ("q".equals(in)){
                        break;
                    }
                    ExpiringMap<String, String> nodeMap = ContextUtil.getNodeMap();
                    List<String> list = new ArrayList<>(nodeMap.keySet());
                    list.add(url);
                    String[] ids = in.split(" ");
                    for (String id : ids) {
                        int andIncrement = no.getAndIncrement();
                        url = list.get(andIncrement%list.size());
                        try {
                            Response response = HttpClient.get(!url.startsWith(AppConstant.HTTP_PREFIX)?(AppConstant.HTTP_PREFIX+url):url+"/weather/"+id, null, Collections.singletonMap(AppConstant.LAMPORT_HEADER_NAME, ContextUtil.getLamportClock().getLatestTime() + ""));
                            System.out.println(response);
                        }catch (IOException e){
                            System.out.println("GETClient get, IOException:"+e.getMessage());
                        }
                    }
                }catch (Exception e){
                    System.out.println("GETClient get, Exception:"+e.getMessage());
                }
            }
        }
    }

}
